﻿using KZ.Express.ENT;
using KZ.Express.Presenter;
using KZ.Express.Resource;
using KZ.Utilities;
using System;
using System.Data;

namespace KZ.Express.Setup.Partner
{
    public partial class AccountDetailView : FormBase
    {
        #region Private Variables

        private new AccountDetailPresenter _presenter;

        #endregion Private Variables

        #region Private Methods

        private void CustomerDetailView_Load(object sender, EventArgs e)
        {
            LoadCarrier();
            LoadData();
        }

        protected override void LoadData()
        {
            try
            {
                switch (base.Action)
                {
                    case ActionName.Update:
                        _presenter.LoadEntity();
                        break;
                }
            }
            catch (BusinessLogicException ex)
            {
                SetMessage(ex.Message, false);
            }
        }

        private void LoadCarrier()
        {
            try
            {
                Global.LoadComboBox(cmbCarrierID, _presenter.LoadForCMB<CarrierENT>(null));
            }
            catch (BusinessLogicException ex)
            {
                SetMessage(ex.Message, false);
            }
        }

        #endregion Private Methods

        #region Constructors

        public AccountDetailView()
        {
            InitializeComponent();
            base.ObjectID = (int)Objects.Account;
            base._presenter = _presenter = new AccountDetailPresenter(this);
        }

        #endregion Constructors

        #region Public Properties

        public override EntityBase Entity
        {
            get
            {
                SupplierENT supplier = new SupplierENT();
                supplier.PartnerID = base.EntityID;
                supplier.PartnerCode = txtAccountCode.Text;
                supplier.PartnerName = txtAccountName.Text;
                supplier.CarrierID = Utility.IntParse(cmbCarrierID.SelectedValue);
                supplier.IsCustomer = false;
                supplier.IsSupplier = true;

                return supplier;
            }
            set
            {
                if (value == null)
                {
                    ClearControl();
                    LoadData();
                }
                else
                {
                    SupplierENT supplier = value as SupplierENT;

                    txtAccountCode.Text = supplier.PartnerCode;
                    txtAccountName.Text = supplier.PartnerName;
                    cmbCarrierID.SelectedValue = supplier.CarrierID ?? 0;
                }
            }
        }

        public DataTable Carriers
        {
            set { cmbCarrierID.DataSource = value; }
        }

        #endregion Public Properties
    }
}